package canon;

import frame.*;
import tree.stm.StmList;

public class Canonlize {

        private static FragList canonlize(FragList frags ) {
                for(int i = 0 ; i < frags.size(); i++){
                        Frag f = frags.get(i);
                        f.setBody( Canon.linearize(f.getBody().get(0)));
                }
                return frags;
        }

        public static StmListList generateCode(FragList frags){
                frags = canonlize(frags);
                StmListList stml = null;
                StmListList aux;
                for(int i = 0 ; i < frags.size(); i++){
                        Frag f = frags.get(i);
                        aux = new BasicBlocks(f.getBody()).blocks;
                        for(; aux!= null; aux = (StmListList) aux.tail){
                                stml = new StmListList(aux.head, stml);
                        }
                }
                return stml;
        }

        public static StmList generateCode(StmListList list){
                StmList stml = null;
                StmListList aux;
                for(aux = list; aux!= null; aux = (StmListList) aux.tail){
                        StmList aux2 = aux.head;
                        for(; aux2!= null; aux2 = (StmList) aux2.tail){
                                stml = new StmList(aux2.head, stml);
                        }
                }
                return stml;
        }
        
}